/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FindSalg.java
 *
 * Created on Dec 1, 2011, 12:18:21 PM
 */
package GUILayer;

import ControlLayer.CtrSalesLine;
import ModelLayer.Sale;
import ModelLayer.SalesLine;
import java.util.ArrayList;

/**
 *
 * @author R3l4X
 */
public class FindSalg extends javax.swing.JFrame {

    /** Creates new form FindSalg */
    public FindSalg() {
        initComponents();
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        btnFindSalgFindID = new javax.swing.JButton();
        txtFindSalgFindSalgID = new javax.swing.JTextField();
        btnFindSalgLuk = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Find Salg");

        jLabel1.setText("Salg ID");

        btnFindSalgFindID.setText("Find");
        btnFindSalgFindID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFindSalgFindIDActionPerformed(evt);
            }
        });

        txtFindSalgFindSalgID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtFindSalgFindSalgIDActionPerformed(evt);
            }
        });

        btnFindSalgLuk.setText("Luk");
        btnFindSalgLuk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFindSalgLukActionPerformed(evt);
            }
        });

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(btnFindSalgLuk, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtFindSalgFindSalgID, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(btnFindSalgFindID)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(btnFindSalgFindID)
                    .addComponent(txtFindSalgFindSalgID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(btnFindSalgLuk)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnFindSalgFindIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFindSalgFindIDActionPerformed

        updateList();


    }//GEN-LAST:event_btnFindSalgFindIDActionPerformed

    private void btnFindSalgLukActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFindSalgLukActionPerformed
        setVisible(false);
    }//GEN-LAST:event_btnFindSalgLukActionPerformed

    private void txtFindSalgFindSalgIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtFindSalgFindSalgIDActionPerformed
        // TODO add your handling code here:
        updateList();
    }//GEN-LAST:event_txtFindSalgFindSalgIDActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FindSalg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FindSalg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FindSalg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FindSalg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FindSalg().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFindSalgFindID;
    private javax.swing.JButton btnFindSalgLuk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtFindSalgFindSalgID;
    // End of variables declaration//GEN-END:variables


    private void updateList()
    {
        CtrSalesLine ctrSalesLine = new CtrSalesLine();
        int id = Integer.parseInt(txtFindSalgFindSalgID.getText());
        try{
            ArrayList<SalesLine> salesObj = ctrSalesLine.listSalesLines(id);
            buildList(salesObj);
            System.out.println(id);
        }
        catch (Exception ex) {
            System.out.println("Fejl " + ex.getMessage());
        }
    }
    
    private void buildList(ArrayList<SalesLine> sales)
    {
        String [] sale = new String [sales.size()];
        int index = 0;
        for(SalesLine sObj : sales)
        {
            String quantity = String.valueOf(sObj.getQuantity());
            String prod = String.valueOf(sObj.getProduct().getType());
            String status = String.valueOf(sObj.getSale().getStatus());
            String price = String.valueOf(sObj.getPrice());
            if(status.equals("true"))
            {
                status = "Betalt";
            }
            else
            {
                status = "Ikke Betalt";
            }
            
            sale[index] = ( prod + "           " + quantity + "           " + price + "           " + status);
            index++;
        } 
        jList1.setListData(sale);
    }
    
//    private void updateList()
//    {
//        CtrSale ctrSale = new CtrSale();
//        int id = Integer.parseInt(txtFindSalgFindSalgID.getText());
//        try{
//            ArrayList<Sale> salesObj = ctrSale.Sales(id);
//            buildList(salesObj);
//            System.out.println(id);
//        }
//        catch (Exception ex) {
//            System.out.println("Fejl " + ex.getMessage());
//        }
//    }
//    
//    private void buildList(ArrayList<Sale> sales)
//    {
//        String [] sale = new String [sales.size()];
//        int index = 0;
//        for(Sale sObj : sales)
//        {
//            String quantity = String.valueOf(sObj.getSaleLine().getProductType());
//            
//            sale[index] = ( quantity + "           " + "            ");
//            index++;
//        } 
//        jList1.setListData(sale);
//    }


}
